﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Ruanmou.Framework.ExpressionE.ExpressionHelper
{
    public  static  class ExpressionExtend
    {

        public static Expression<Func<T, bool>> Add<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) 
        {
            if (expr1 == null)
                return expr2;
            else if (expr2 == null)
                return expr1;

            //新建的参数
            ParameterExpression parameter = Expression.Parameter(typeof(T), "c");

            NewExpressionVisitor visitor = new NewExpressionVisitor(parameter);

            var left = visitor.Replace(expr1.Body);
            var right = visitor.Replace(expr2.Body);
            var body = Expression.And(left, right); //合并条件

            return Expression.Lambda<Func<T, bool>>(body, parameter);

        }
    }
}
